/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.zumbits.avd.subversion;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 *
 * @author zumbits
 */
public class SvnManager {

    public static final String AVD_SVN = ".avd";
    //
    private String path;

    public SvnManager() {
    }

    public boolean start(String path) {
        //TODO: gerar log
        this.path = path + File.separator + AVD_SVN;
        File avd = new File(this.path);

        if (!avd.exists()) {
            try {
                avd.createNewFile();

            } catch (Exception ex) {
                return false;
            }
        }

        return true;
    }

    public boolean add(String name, int version) {
        File avd = new File(this.path);
        //TODO: melhorar - não permitir a adição de um nome que já existe

        if (avd.exists()) {
            try {
                PrintWriter printWriter = new PrintWriter(new FileWriter(avd, true), true);

                printWriter.println(name + ":" + version);
                printWriter.close();

                return true;
            } catch (Exception ex) {
            }
        }

        return false;
    }

    public boolean delete(String fileName) {
        File avd = new File(this.path);
        boolean ok = false;

        if (avd.exists()) {
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(avd));

                String allLines = "";
                String line = null;
                StringTokenizer lineTokenizer = null;

                while ((line = bufferedReader.readLine()) != null) {
                    lineTokenizer = new StringTokenizer(line);

                    if (fileName.equals(lineTokenizer.nextToken(":"))) {
                        ok = true;
                    } else {
                        allLines += line + "\n";
                    }
                }

                bufferedReader.close();

                PrintWriter printWriter = new PrintWriter(new FileWriter(avd, false));
                printWriter.print(allLines);

                printWriter.close();
            } catch (Exception ex) {
            }
        }

        return ok;
    }

    public int getVersion(String fileName) {
        File avd = new File(this.path);

        if (avd.exists()) {
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(avd));

                String line = null;
                StringTokenizer lineTokenizer = null;

                while ((line = bufferedReader.readLine()) != null) {
                    lineTokenizer = new StringTokenizer(line);
                    if (fileName.equals(lineTokenizer.nextToken(":"))) {
                        bufferedReader.close();
                        return Integer.parseInt(lineTokenizer.nextToken());
                    }
                }
            } catch (Exception ex) {
                //TODO: gerar log
            }
        }

        //Não foi encontrado o arquivo de configuração ou o arquivo fileName
        return -1;
    }

    public boolean setVersion(String fileName, int version) {
        File avd = new File(this.path);
        boolean ok = false;

        if (avd.exists()) {
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(avd));

                String allLines = "";
                String line = null;
                StringTokenizer lineTokenizer = null;

                while ((line = bufferedReader.readLine()) != null) {
                    lineTokenizer = new StringTokenizer(line);

                    if (fileName.equals(lineTokenizer.nextToken(":"))) {
                        ok = true;
                        allLines += fileName + ":" + version + "\n";

                    } else {
                        allLines += line + "\n";
                    }
                }

                bufferedReader.close();

                PrintWriter printWriter = new PrintWriter(new FileWriter(avd, false));
                printWriter.print(allLines);

                printWriter.close();
            } catch (Exception ex) {
            }
        }

        return ok;
    }
}
